<?php
    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
    { 
        private $_cache = null;
        
        protected function _initWZ()
        {
            error_reporting(E_ALL);
            function WZErrorHandler($level, $message, $file, $line)
            {
                $level = $level & error_reporting();

                if ($level == 0) {
                    return false;
                }

                if (!defined('E_STRICT')) {
                    define('E_STRICT', 2048);
                }
                if (!defined('E_RECOVERABLE_ERROR')) {
                    define('E_RECOVERABLE_ERROR', 4096);
                }
                if (!defined('E_DEPRECATED')) {
                    define('E_DEPRECATED', 8192);
                }
                if (!defined('E_USER_DEPRECATED')) {
                    define('E_USER_DEPRECATED', 16384);
                }

                $errorMessage = '';

                switch($level){
                    case E_ERROR:
                        $errorMessage .= 'Error';
                        break;
                    case E_WARNING:
                        $errorMessage .= 'Warning';
                        break;
                    case E_PARSE:
                        $errorMessage .= 'Parse Error';
                        break;
                    case E_NOTICE:
                        $errorMessage .= 'Notice';
                        break;
                    case E_CORE_ERROR:
                        $errorMessage .= 'Core Error';
                        break;
                    case E_CORE_WARNING:
                        $errorMessage .= 'Core Warning';
                        break;
                    case E_COMPILE_ERROR:
                        $errorMessage .= 'Compile Error';
                        break;
                    case E_COMPILE_WARNING:
                        $errorMessage .= 'Compile Warning';
                        break;
                    case E_USER_ERROR:
                        $errorMessage .= 'User Error';
                        break;
                    case E_USER_WARNING:
                        $errorMessage .= 'User Warning';
                        break;
                    case E_USER_NOTICE:
                        $errorMessage .= 'User Notice';
                        break;
                    case E_STRICT:
                        $errorMessage .= 'Strict Notice';
                        break;
                    case E_RECOVERABLE_ERROR:
                        $errorMessage .= 'Recoverable Error';
                        break;
                    case E_DEPRECATED:
                        $errorMessage .= 'Deprecated functionality';
                        break;
                    case E_USER_DEPRECATED:
                        $errorMessage .= 'User-generated warning message';
                        break;
                    default:
                        $errorMessage .= "Unknown error ($level)";
                        break;
                }

                $errorMessage .= ": {$message}  in {$file} on line {$line}";
                throw new Exception($errorMessage);
            }
            set_error_handler('WZErrorHandler');
        }
        
        protected function _initView ()
        {
            $view = new Zend_View();
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
            $viewRenderer->setView($view);
            return $view;
        }
        
        protected function _initAppAutoLoad()
        {
            $autoLoader = Zend_Loader_Autoloader::getInstance();
            $autoLoader->registerNamespace('WZ_');
            
            $resourceLoader = new Zend_Loader_Autoloader_Resource(
                array(
                    'basePath' => APPLICATION_PATH, 
                    'namespace' => '', 
                    'resourceTypes' => array(
                        'form' => array(
                            'path' => 'forms/', 
                            'namespace' => 'WZForm_'
                        ), 
                        'plugin' => array(
                            'path' => 'plugins/', 
                            'namespace' => 'WZPlugin_'
                        ), 
                        'service' => array(
                            'path' => 'services/', 
                            'namespace' => 'WZService_'
                        ), 
                        'model' => array(
                            'path' => 'models/', 
                            'namespace' => 'WZModel_'
                        )
                    )
                )
            );
            return $autoLoader;
        }
        
        protected function _initSettings() 
        {
            Zend_Registry::set('WZBootstrap', $this);
            Zend_Registry::set('WZConfigPath', APPLICATION_PATH . '/configs/');
            Zend_Registry::set('WZFormsPath', APPLICATION_PATH . '/forms/');
            Zend_Registry::set('WZModelsPath', APPLICATION_PATH . '/models/');
            Zend_Registry::set('WZPluginsPath', APPLICATION_PATH . '/plugins/');
            Zend_Registry::set('WZServicesPath', APPLICATION_PATH . '/services/');
            Zend_Registry::set('WZDebugMode', $this->getOption('debugMode'));
            Zend_Registry::set('WZApp', $this->getApplication());
            
            try {
			Zend_Session::start(true);
            } catch (Exception $oException) {
                Zend_Session::isRegenerated() || Zend_Session::regenerateId();
                $_COOKIE = array();
            }
            
            if ($this->hasPluginResource('view')) {
                $this->bootstrap('view');
                $view = $this->getPluginResource('view')->getView();
                Zend_Registry::set('WZview', $view);
                $view->addHelperPath(APPLICATION_PATH.'/../libs/wz/Helpers/Views', 'WZ_Views_Helper');
                $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
                $viewRenderer->setView($view);
                Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
            }

            $this->unregisterPluginResource('layout');
            $front = $this->bootstrap('frontController');
            
            $this->bootstrap('cachemanager');
            $cacheManager = $this->getResource('cachemanager');
            Zend_Registry::set('cacheManager', $cacheManager);
            Zend_Registry::set('WZFront', $front);

            Zend_Filter::addDefaultNamespaces(array(
               'WZ_Filter' 
            ));
        
            if (true == $this->getOption('enablePluginLoaderCache')) {
                $WZPlugin = APPLICATION_PATH . '/libs/wz/Plugins/UtilsPlugin.php';

                if (file_exists($WZPlugin)) {
                    include_once $WZPlugin;
                }
                
                $front = Zend_Controller_Front::getInstance();
                $front->registerPlugin(new WZ_Utils());
            }
            
            $frontendOptions = array(
                'lifetime' => 7200,
                'automatic_serialization' => true
            );
            
            $backendOptions = array(
                'cache_dir' => APPLICATION_PATH . '/cache'
            );
            Zend_Registry::set('WZCache', Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions));
            
            $autoloader = $this->_application->getAutoloader();
            require_once APPLICATION_PATH . '/../../../lib/Doctrine/Doctrine/Common/ClassLoader.php';
            $entitiesAutoloader = new \Doctrine\Common\ClassLoader('Doctrine', APPLICATION_PATH . '/models');
            $autoloader->pushAutoloader(array($entitiesAutoloader, 'loadClass'), 'Doctrine');
            require_once(APPLICATION_PATH . '/../../../lib/ezComponents/Base/src/base.php');
            $autoloader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');
        }
        
        protected function _initWZDB() 
        {
            $this->bootstrap('db');
            $db = $this->getPluginResource('db');
            $stmt = new Zend_Db_Statement_Pdo(
                $db->getDbAdapter(),
                "SET NAMES 'utf8'"
            );
            $stmt->execute();
            $dbAdapter = $db->getDbAdapter();
            Zend_Registry::set('ZendDb', $dbAdapter);
        }
        
        protected function _initFrontModules() 
        {
            $this->bootstrap('frontController');
            $front = $this->getResource('frontController');
            $front->addModuleDirectory(APPLICATION_PATH . '/modules');
            
            $options = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
            $router = $front->getRouter();
            $router->addConfig($options);
        }
        
        protected function _initLog() 
        {
            if ($this->getOption('debug_mode') && $this->hasPluginResource('log')) {
                $this->bootstrap('log');
                Zend_Registry::set('logger', $this->getResource('log'));
            }
        }
        
        protected function _initSession() 
        {
            $session = new Zend_Session_Namespace('WZSession', true);
            Zend_Registry::set('WZSession', $session);
            return $session;
        }

        protected function _initConfig() 
        {
            $config = new Zend_Config($this->getOptions(), true);
            Zend_Registry::set('config', $config);
            return $config;
        }
    }
// do not use PHP closing tag unless needed
